#-*- coding:utf-8 -*-
# 倒包
from appium import webdriver
# 导入time包
import time
# 导入枚举包
from enum import Enum
# 倒包
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

#声明类
class AppUtil(object):


    #  打开手机的方法
    def AppStart(self):
        # 定义字典配置 appium的参数
        qiezi = {}

        # 给字典字段
        # 指定工具
        qiezi['platformName'] = 'Android'
        # 指定版本号
        qiezi['platformVersion'] = '4.4'
        # 指定自动化测试工具
        qiezi['automationName'] = 'Appium'
        # 指定手机的唯一识别码
        qiezi['deviceName'] = '6O5506A01196'
        # 指定apk的安装录音
        qiezi['app'] = '/Users/apple/Desktop/keystore/com.qiezzi.eggplant.apk'
        # 指定app的包名
        qiezi['appPackage'] = 'com.qiezzi.eggplant'
        # 指定默认启动的activity
        qiezi['appActivity'] = 'com.qiezzi.eggplant.base.WelcomeActivity'
        # 等待启动的包名
        qiezi['appWaitPackage'] = 'com.qiezzi.eggplant'
        # 等待启动的activity
        qiezi['appWaitActivity'] = 'com.qiezzi.eggplant.base.WelcomeActivity'

        # 关闭键盘的命令
        # 关闭app的键盘
        qiezi['resetKeyboard'] = True
        # 使用 unicodeKeyboard 编码格式来发送字符串
        qiezi['unicodeKeyboard'] = True
        # 当设置为true的时候,不会再进行apk的安装
        qiezi['noReset'] = True

        # 启动 app

        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", qiezi)

        # 休眠五秒
        self.Time_Sleep(ENUMS.TEN_TIME)

        pass

#     关闭
    def App_Close(self):

        self.driver.quit()

        pass

    #  休眠方法
    def Time_Sleep(self,number):

        time.sleep(number)

        pass

    # 隐士xium
    def Time_Implay(self,number):

        self.driver.implicitly_wait(number)

        pass

    # 封装滑动到最后点击立即立即体验
    def Scrool(self):


        # 使用for循环进行
        for index in range(0,4):

            if index < 3:
                # 休眠俩秒
                self.Time_Sleep(ENUMS.TWN_TIME)
                # 滑动一次
                self.driver.swipe(self.getWidth()-50,self.getHeight()/2,50,self.getHeight()/2,1000)

            else:

                # 休眠俩秒
                self.Time_Sleep(ENUMS.TWN_TIME)
                # 开始查找控件
                btn_feel_right_now = self.FindID("com.qiezzi.eggplant:id/btn_feel_right_now")
                # 点击一下
                btn_feel_right_now.click()

                pass


        pass

    # 封装ID查找控件的方法
    def FindID(self,ID):

        ids = (By.ID,ID)

        # 休眠
        WebDriverWait(self.driver,ENUMS.TWNTENTY,ENUMS.ONE_HALF).until(EC.presence_of_element_located(ids))

        return self.driver.find_element_by_id(ID)
        pass

    # 点击跳转
    def ClidkID(self,ID):

        self.FindID(ID).click()

        pass
    # 通过ID输入内容
    def SendkeysID(self,ID,content):

        self.FindID(ID).send_keys(content)

        pass

    #  获取手机屏幕宽度和高度的方法
    def getWidth(self):

        return self.driver.get_window_size()['width']

    def getHeight(self):

        return self.driver.get_window_size()['height']


#    获取当前acti
    def getCurrent(self):

        return self.driver.current_activity

    def find_toast(self, message):

        '''''判断toast信息'''

        try:

            element = WebDriverWait(self.driver, 20,0.5).until(
                EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, message)))

            return True
        except:

            return False


# 定义枚举类型
class ENUMS(Enum):

#     休眠五秒
    FIVE_TIME = 5
# 10 秒
    TEN_TIME = 10
# 二秒
    TWN_TIME = 2
# 20 秒
    TWNTENTY = 20
# 0.5秒
    ONE_HALF = 0.5








